// pages/index/index.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [], //轮播图数据
    recommendList: [], //推荐歌单数据
    topList: [], //排行榜数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    // 轮播图数据
    let bannerListData = await request('/banner', {
      type: 2
    });
    this.setData({
      bannerList: bannerListData.banners
    })
    //推荐歌单数据
    let recommendListData = await request('/personalized', {
      limit: 10
    })
    this.setData({
      recommendList: recommendListData.result
    })                                                          
    //排行榜数据
    let index = 0;
    let resultArr = [];
    while (index < 5) {
      let topListData = await request('/top/list', {
        idx: index++
      });
      let topListItem = {
        name: topListData.playlist.name,
        tracks: topListData.playlist.tracks.slice(0, 3)
      };
      resultArr.push(topListItem);
      //拿到数据就渲染到页面，但是渲染次数多
      this.setData({
        topList: resultArr
      })
    }
  },

  //跳转到每日推荐歌曲页面
  toRecommendSong(){
    wx.navigateTo({
      url: '/pages/recommendSong/recommendSong',
    })
  },
  
  //跳转到搜索页面
  toSearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

  // 跳转歌曲详情
  toSongDetail(event){
    wx.navigateTo({
      url: '/pages/songDetail/songDetail?song=' + event.currentTarget.id
    })
  },

  //跳转到歌单歌曲列表页面
  toPlayList(event){
    wx.navigateTo({
      url: '/pages/playlist/playlist?id=' + event.currentTarget.id
    })
  },

})